// pages/test/test.js
// const db = wx.cloud.database({
//   env: 'new-luoshishi-pe6zm'
// })
Page({

  /**
   * 页面的初始数据
   */
  data: {
    top: 0,
    flag:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.get_check()
    let obj = wx.getMenuButtonBoundingClientRect()
    this.setData({
      top: obj.top
    })
  },

  // 获取审核数字
  get_check(){
    db.collection('check').get().then(res => {
      this.setData({
        flag:res.data[0].content
      })
    })
  },

  // 获取数据
  nav_detail(e) {
    let url = e.currentTarget.dataset.url
    wx.navigateTo({
      url: url,
    })
  },

  // 订阅消息
  sub_tips(e) {
    var that = this
    var value = wx.getStorageSync('sub')
    wx.showLoading({
      title: '加载中',
    })
    if (value) { // 本地有记录，跳转
      that.nav_mini(e)
    } else { //本地没有记录，唤起订阅消息并本地记录
      wx.requestSubscribeMessage({
        tmplIds: ['GgFp5_w8IYmRpagK8IXUlfUfZwjsNAmnG0S6wuVcpss', 'MWx5yhfe-bq_8dTY2JmH-R06pT7qE3MyNAyw_RFbQIA'],
        success(res) {
          that.nav_mini(e)
          wx.cloud.callFunction({
            name: 'add_user',
            success(res) {
              console.log('发请求成功')
            }
          })
          wx.setStorage({
            key: "sub",
            data: "login"
          })
        }
      })
    }
    wx.hideLoading()
  },

  // 领取红包
  nav_mini(e) {
    var type = e.currentTarget.dataset.type
    var path = ''
    var id = ''
    var that = this
    if (type == 1) {
      id = 'wxece3a9a4c82f58c9'
      path = "ele-recommend-price/pages/guest/index?chInfo=ch_wechat_chsub_Card&_ltracker_f=&inviterId=98e959f"
    } else if (type == 2) {
      id = 'wxece3a9a4c82f58c9'
      path = "taoke/pages/shopping-guide/index?scene=dBkV6ku"
    } else if (type == 3) {
      id = 'wx2c348cf579062e56'
      path = "outer_packages/r2xinvite/coupon/coupon?inviteCode=NnOIp-QOs8SiYF1dcSlL5r8phPrCf6qkH7evMyjIoup2NXxNCLYcBbd3bqpv2X2IkP84iu9RDt0ByI5WVKnmE_pc0u4Ds8Tr4YDQjQ9URZZ55n7T0Y6V3fj7hAGdMgef5g7Zq79Fa25_AdjNWgltSGGaXqRfd8wSe_kjDAU8Wjo"
    } else if (type == 4) {
      id = 'wxece3a9a4c82f58c9'
      path = "pages/sharePid/web/index?scene=s.click.ele.me%2FKyADSju"
    }else if (type == 5) {
      id = 'wxbc0cf9b963bd3550'
      path = "shareActivity/basic_activity/page/BasicActivityPop/BasicActivityPop?page_id=4k1Khw5X8wy&gd_from=outside_coupon_&pid=mm_1381580118_2002350272_110793600277"
    }
    wx.navigateToMiniProgram({
      appId: id,
      path: path,
      success: function () {},
    })
  },

  nav_tips() {
    wx.showModal({
      title: '提示',
      content: "该功能暂未开放~先来领个红包吧"
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: "给朋友圈的朋友们安利一个宝藏小程序，每日抓取最新的外卖优惠券，最高减66元",
      imageUrl: "https://6e65-new-luoshishi-pe6zm-1301600223.tcb.qcloud.la/rice.png?sign=7e7d6dd5344b44e43ad6bab2f82d8de6&t=1628995793"
    };
  },

  onShareTimeline: function () {
    return {
      title: "给朋友圈的朋友们安利一个宝藏小程序，每日抓取最新的外卖优惠券，最高减66元",
      imageUrl: "https://6e65-new-luoshishi-pe6zm-1301600223.tcb.qcloud.la/rice.png?sign=7e7d6dd5344b44e43ad6bab2f82d8de6&t=1628995793"
    };
  }
})